/*******************************************************************************
 * Copyright 2020 huanggefan.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package core

import (
	"flag"
	"fmt"
)

func PublicFlag(tagVer string, goVer string, timeVer string) {
	BuildTag = tagVer
	BuildGo = goVer
	BuildTime = timeVer

	flag.BoolVar(&FlagHelp, "h", false, "show help information")
	flag.BoolVar(&FlagVersion, "v", false, "show version information")

	flag.BoolVar(&FlagUseSqlite, "use-sqlite", false, "use sqlite as database")
	flag.BoolVar(&FlagUsePG, "use-pg", false, "use postgresql as database")

	flag.StringVar(&FlagSqliteDB, "sqlite-db", "./sqlite.db", "sqlite database file")

	flag.StringVar(&FlagPgAddr, "pg-addr", "127.0.0.1", "postgresql database address")
	flag.IntVar(&FlagPgPort, "pg-port", 5432, "postgresql database address port")
	flag.StringVar(&FlagPgDB, "pg-db", "postgres", "postgresql database")
	flag.StringVar(&FlagPgUser, "pg-user", "postgres", "postgresql user")
	flag.StringVar(&FlagPgPassword, "pg-password", "postgres", "postgresql password")
	flag.StringVar(&FlagPgSSLMode, "pg-sslmode", "disable", "postgresql ssl mode")
	flag.StringVar(&FlagPgSSLCert, "pg-sslcert", "./server.crt", "postgresql ssl public certificate")
	flag.StringVar(&FlagPgSSLKey, "pg-sslkey", "./server.key", "postgresql ssl private key")
	flag.StringVar(&FlagPgSSLRootCert, "pg-sslrootcert", "./root.crt", "postgresql CA public certificate")

	flag.IntVar(&FlagLogLevel, "log", LogInfo, "log level, 0-3, mean: debug, info, warning, error")

	flag.Usage = func() {
		fmt.Printf("Version: %s Go Version: %s BuildAT: %s LICENSE: %s\n\n",
			BuildTag, BuildGo, BuildTime, "Apache License Version 2.0")
		flag.PrintDefaults()
		fmt.Println("PostgreSQL SSLMode: 'disable', 'allow', 'prefer', 'require', 'verify-ca', 'verify-full'")
		fmt.Println("    When SSLMode is: 'verify-ca',   need: '-pg-sslrootcert'")
		fmt.Println("    When SSLMode is: 'verify-full', need: '-pg-sslrootcert', '-pg-sslcert', '-pg-sslkey'")
	}
}

func PublicVersion() {
	fmt.Printf("Version: %s\n", BuildTag)
	fmt.Printf("Go Version: %s\n", BuildGo)
	fmt.Printf("BuildAT: %s\n", BuildTime)
	fmt.Printf("LICENSE: %s\n", "Apache License Version 2.0")
}

func PublicServerFlag() {
	flag.StringVar(&FlagHttpListen, "listen-http", "127.0.0.1:6001", "http listen address")
	flag.StringVar(&FlagGRPCListen, "listen-grpc", "127.0.0.1:7001", "grpc listen address")

	flag.BoolVar(&FlagPPROFEnable, "pprof-enable", false, "enable pprof debug")
	flag.StringVar(&FlagPPROFListen, "pprof-listen", "127.0.0.1:8001", "pprof debug address")

	flag.BoolVar(&FlagLogWithUDP, "log-with-udp", false, "enable log data to udp")
	flag.StringVar(&FlagLogAddr, "log-addr", "127.0.0.1:9001", "udp log server addr")

	flag.StringVar(&FlagTokenCert, "token-cert", "", "for generating secure token")
}
